<template>
  <div class="post-comment">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="优质评论将会被优先展示"
      show-word-limit
    />
    <van-button type="primary" size="small" @click="surePublish">
      发布
    </van-button>
  </div>
</template>

<script>
// 导入 用户 发表对文章 评论 的接口
import { publishComment } from '@/api/commen.js'
export default {
  name: 'PostComment',
  components: {},
  props: {
    target: {
      type: [Number, String],
      required: true
    },
    commentData: {
      type: Array
    }
  },
  data() {
    return {
      message: ''
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async surePublish() {
      // 如果 输入框 没有文本 那么退出函数
      if (this.message.length === 0) return this.$toast('请输入文本内容')
      try {
        const { data } = await publishComment(this.target, this.message)
        this.commentData.unshift(data.data.new_obj)
        console.log(this.commentData)
        this.$toast.success('评论发布成功~~')
      } catch (err) {
        this.$toast.fail('评论失败！')
      }
      // 发布成功后 清除 文本输入框内容
      this.message = ''
      // 关闭 评论 弹窗
    }
  }
}
</script>

<style scoped lang="less">
.post-comment {
  display: flex;
  padding: 15px;
  align-items: flex-end;
  .post-field {
    background: #f5f7f9;
    margin-right: 15px;
  }
}
</style>
